<template>
    <div style="display: flex;justify-content: center;align-items: center;margin-top: 5%">
        <!-- -->
        <el-card style="width: 50%;align-items: center;">
            <div style="display: flex;align-items: center;justify-content: center;padding-bottom:20px ;">
                <el-upload
                        class="avatar-uploader"
                        action='http://localhost:7070/api/file/upload'
                        :headers="{token:user.token}"
                        :on-success="handleAvatarSuccess"
                        :show-file-list="false"
                >
                    <img v-if="user.avatar" :src="user.avatar" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </div>
            <el-form :model="user" label-width="80px" style="padding-right: 20px;">
                <el-form-item label="用户名" prop="username">
                    <el-input disabled v-model="user.username" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="user.name" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="user.phone" placeholder="电话"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="user.email" placeholder="邮箱"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input type="textarea" v-model="user.address" placeholder="地址"></el-input>
                </el-form-item>
                <div style="text-align: center;">
                    <el-button size="medium " type="primary" @click="update">保 存</el-button>
                </div>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import { ref } from 'vue';

export default {
    data() {
        return {
            user:JSON.parse(localStorage.getItem("honey-user"||'{}'))
        };
    },
    methods:{
        handleAvatarSuccess(response, file, fileList){
            // 把user的头像属性换成上传图片的连接
            this.user.avatar=response.data
        },
        // 保存当前用户信息到数据库
        // 更新浏览器缓存里的用户信息
        update() {
            this.$request.put('/user/update',this.user).then( res=> {
                if (res.code === '200'){

                    this.$message.success('保存成功')
                    localStorage.setItem('honey-user', JSON.stringify(this.user))

                    // 触发父级的数据更新
                    this.$emit('update:user', this.user);
                }else{
                    this.$message.error("res.msg")
                }
            })
        }
    }
};
</script>
<style scoped>
/deep/.el-form-item__label {
    font-weight: bold;
}
/deep/.el-upload {
    border-radius: 50%;
}
/deep/.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    border-radius: 50%;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border-radius: 50%;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius: 50%;
}
</style>